﻿using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;


namespace lu596 {
    partial class ThietBiDataSet
    {
        partial class DAY_DOIDataTable
        {
            public void KetNoiDAYDOI(int CAP_ID_TRUOC, int CAP_ID, int num)
            {
                //Số đôi của CAP_ID_DAU còn lại chưa có kết nối với các đôi kế tiếp
                string sql = "select top " + num.ToString() + " DAY_DOI.* from DAY_DOI, NHOM_DOI, CAP" +
                            " where DAY_DOI.NHOM_DOI_ID = NHOM_DOI.NHOM_DOI_ID" +
                            " and NHOM_DOI.CAP_ID = CAP.CAP_ID" +
                            " and CAP.CAP_ID = " + CAP_ID_TRUOC.ToString() + 
                            " and DAY_DOI.DAY_DOI_ID not in " +
                            " (select DAY_DOI_TRUOC_ID from DAY_DOI where DAY_DOI_TRUOC_ID is not null)" +
                            " order by DAY_DOI.DAY_DOI_ID";
                DataSet dsDAY_DOI_TRUOC = MITI.BaseDB.ExecSql_DataSet(sql);

                //Số đôi của CAP_ID_CUOI sẽ kết nối với CAP_ID_DAU
                sql = "select top " + num.ToString() + " DAY_DOI.* from DAY_DOI, NHOM_DOI, CAP" +
                        " where DAY_DOI.NHOM_DOI_ID = NHOM_DOI.NHOM_DOI_ID" +
                        " and NHOM_DOI.CAP_ID = CAP.CAP_ID" +
                        " and CAP.CAP_ID = " + CAP_ID.ToString();
                DataSet dsDAY_DOI = MITI.BaseDB.ExecSql_DataSet(sql);

                //Kết nối 
                sql = "";
                foreach(DataRow drDAY_DOI in dsDAY_DOI.Tables[0].Rows)
                {
                    foreach(DataRow drDAY_DOI_TRUOC in dsDAY_DOI_TRUOC.Tables[0].Rows){
                        sql += "update DAY_DOI" +
                            " set DAY_DOI_TRUOC_ID = " + drDAY_DOI_TRUOC["DAY_DOI_ID"].ToString() +
                            " where DAY_DOI_ID = " + drDAY_DOI["DAY_DOI_ID"].ToString() + 
                            " go ";
                    }
                    
                }
                MITI.BaseDB.ExecSql_MultiQueries(sql);
            }
        }
        partial class CAPDataTable
        {
            public DataView GetCAP(int cAP_ID)
            {
                ThietBiDataSet.CAPDataTable capTable = new ThietBiDataSet.CAPDataTable();
                ThietBiDataSetTableAdapters.CAPTableAdapter cAPTableAdapter = new lu596.ThietBiDataSetTableAdapters.CAPTableAdapter();
                
                cAPTableAdapter.Fill(capTable);
                DataView view = capTable.DefaultView;
                view.RowFilter = "CAP_ID=" + cAP_ID.ToString();
                return view;
            }

            public List<int> GetDAYDOIInfoByCAP(int cAP_ID)
            {
                List<int> result = new List<int>();
                //Tổng số dây đôi của CAP_ID
                string sql = "select count(*) from DAY_DOI, NHOM_DOI, CAP" +
                " where DAY_DOI.NHOM_DOI_ID = NHOM_DOI.NHOM_DOI_ID" +
                " and NHOM_DOI.CAP_ID = CAP.CAP_ID" +
                " and CAP.CAP_ID = " + cAP_ID.ToString();
                int value = (int)MITI.BaseDB.ExecSql_DataValue(sql);
                result.Add(value);

                //condition query
                string sqlCondition = "select DAY_DOI_ID from DAY_DOI, NHOM_DOI, CAP" +
                             " where DAY_DOI.NHOM_DOI_ID = NHOM_DOI.NHOM_DOI_ID" +
                             " and NHOM_DOI.CAP_ID = CAP.CAP_ID" +
                             " and CAP.CAP_ID = " + cAP_ID.ToString();
                //Số đôi của CAP_ID đã được kết nối
                sql = "select count(*) from DAY_DOI dd" +
                      " where dd.DAY_DOI_TRUOC_ID in" +
                      " (" + sqlCondition + ")";

                value = (int)MITI.BaseDB.ExecSql_DataValue(sql);
                result.Add(value);

                //Số đôi của CAP_ID chưa được kết nối
                value = result[0] - result[1];
                result.Add(value);

                return result;
            }

            
        }
    
        partial class DUNG_LUONG_CAPDataTable
        {
            public DataView GetDungLuongCap(int dungLuongCap_Id)
            {
                ThietBiDataSet.DUNG_LUONG_CAPDataTable tableDungLuongCap = new ThietBiDataSet.DUNG_LUONG_CAPDataTable();
                lu596.ThietBiDataSetTableAdapters.DUNG_LUONG_CAPTableAdapter dUNG_LUONG_CAPTableAdapter = new lu596.ThietBiDataSetTableAdapters.DUNG_LUONG_CAPTableAdapter();
                dUNG_LUONG_CAPTableAdapter.Fill(tableDungLuongCap);
                DataView view = tableDungLuongCap.DefaultView;
                view.RowFilter = "DUNG_LUONG_CAP_ID=" + dungLuongCap_Id.ToString();
                return view;
            }
        }
    
        partial class NHOM_DOIDataTable
        {

        }

        
    }
}

namespace lu596.ThietBiDataSetTableAdapters
{
    public partial class CAPTableAdapter : global::System.ComponentModel.Component
    {
        public SqlCommand[] SelectComand
        {
            get
            {
                if (_commandCollection == null)
                    InitCommandCollection();
                return _commandCollection;
            }
            set
            {
                _commandCollection = (SqlCommand[])value;
            }
        }

        public int FillByWhere(ThietBiDataSet.CAPDataTable db, string whereExp){
            string stSelect = _commandCollection[0].CommandText;
            try
            {
                _commandCollection[0].CommandText += " where " + whereExp;
                return this.Fill(db);
            }
            catch
            {
            }
            finally
            {
                this._commandCollection[0].CommandText = stSelect;
            }

            return -1;
        }
    }
}
